package mo.leetcode_top;

public class L165 {

    public int compareVersion(String version1, String version2) {

        String[] split1 = version1.split("\\.");
        String[] split2 = version2.split("\\.");

        int length1 = split1.length;
        int length2 = split2.length;
        int min = Math.min(length1, length2);

        for (int i = 0; i < min; i++) {
            int v1 = Integer.parseInt(removeZero(split1[i]));
            int v2 = Integer.parseInt(removeZero(split2[i]));
            if (v1 < v2) {
                return -1;
            } else if (v1 > v2) {
                return 1;
            }
        }

        if (length1 > min) {
            while (min < length1) {
                int v = Integer.parseInt(removeZero(split1[min]));
                if (v > 0) {
                    return 1;
                }
                min++;
            }
        }

        if (length2 > min) {
            while (min < length2) {
                int v = Integer.parseInt(removeZero(split2[min]));
                if (v > 0) {
                    return -1;
                }
                min++;
            }
        }
        return 0;
    }

    private static String removeZero(String s) {
        if (s.length() == 1) {
            return s;
        }
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] != '0') {
                return s.substring(i);
            }
        }
        return s.substring(0, 1);
    }

    public static void main(String[] args) {
        L165 test = new L165();
        System.out.println(test.compareVersion("1", "1.1"));
    }

}
